<?php
/**
 * Register controller.
 *  
 * @author Nguyen Hai Ha
 * @version 1.0
 */

class Account_RegisterController extends Zend_Controller_Action {
	protected $_baseUrl;
	protected $_contextPath;
	protected $_utilities;	
	protected $_errors;
	
	public function init() {
		$this->_baseUrl = $this->_request->getBaseURL ();
		$this->_contextPath = "account/register";
		
		$this->view->assign ( "baseUrl", $this->_baseUrl );
		$this->view->assign ( "contextPath", $this->_contextPath );
		
		$this->_utilities = new Utilities ( );
		$this->view->assign ( "utilities", $this->_utilities );
		$this->view->assign ( "pageTitle", "EzGSM : Đăng ký tài khoản" );
		$this->_errors = array ();
	}

	public function captchaAction(){
		$this->_helper->viewRenderer->setNoRender();
		
		$img = new Captcha_Securimage();
		$img->show();
	}
	
	public function indexAction() {
		if ($this->_request->isPost ()) {
			// Save member.
			$f = new Zend_Filter_StripTags ( );
			
			$usr_mobile = $f->filter ( $this->_request->getPost ( "usr_mobile" ) );
			$usr_password = $f->filter ( $this->_request->getPost ( "usr_password" ) );
			$usr_password2 = $f->filter ( $this->_request->getPost ( "usr_password2" ) );
			$usr_fullname = $f->filter ( $this->_request->getPost ( "usr_fullname" ) );
			$usr_email = $f->filter ( $this->_request->getPost ( "usr_email" ) );
			$usr_coin_amount = 0;
			$usr_activated = 0;
			$usr_activated_date = null;
			$usr_created_date = date ( 'Y-m-d H:m:s' );
			$usr_city = ( int ) $f->filter ( $this->_request->getPost ( "usr_city" ) );
			$captcha = $f->filter ( $this->_request->getPost ( "captcha" ) );
			
			$day = $f->filter ( $this->_request->getPost ( "day" ) );
			$month = $f->filter ( $this->_request->getPost ( "month" ) );
			$year = $f->filter ( $this->_request->getPost ( "year" ) );
			$usr_birthday = $year."-".$month."-".$day; 
			
			$salt = 'klmn01234opqrst';

			$account = array ('usr_mobile' => $usr_mobile, 'usr_password' => md5(md5 ( $usr_password.$salt )), 'usr_fullname' => $usr_fullname, 'usr_birthday' => $usr_birthday, 'usr_email' => $usr_email, 'usr_activated' => $usr_activated, 'usr_activated_date' => $usr_activated_date, 'usr_role' => Constant::MEMBER, 'usr_created_date' => $usr_created_date, 'usr_modified_date' => $usr_created_date, 'usr_coin_amount' => $usr_coin_amount, 'usr_city' => $usr_city );
			
			$userObject = new UserModel ( );
			
			// Begin validate.
			

			/* Validate mobile */
			if (! strlen ( $usr_mobile ))
				$this->_errors ['mobile_null'] = "Bạn vui lòng nhập số điện thoại.";
			else {
				if ($userObject->checkUserExist ( $usr_mobile ))
					$this->_errors ['mobile_exist'] = "Số điện thoại đã tồn tại.";
				if (! $this->_utilities->check_mobile ( $usr_mobile ))
					$this->_errors ['mobile_incorrect'] = "Số điện thoại không hợp lệ.";
			}
			/* Validate password */
			if (! strlen ( $usr_password ))
				$this->_errors ['password_null'] = "Bạn vui lòng nhập mật khẩu.";
			else {
				if (strlen ( $usr_password ) < 6)
					$this->_errors ['password_incorrect'] = "Mật khẩu phải lớn hơn 6 kí tự.";
				else if ($usr_password != $usr_password2)
					$this->_errors ['password2_incorrect'] = "Mật khẩu không khớp nhau.";
			}
			
			/* Validate fullname */
			if (! strlen ( $usr_fullname ))
				$this->_errors ['fullname_null'] = "Bạn vui lòng nhập họ tên.";
				
			/* Validate email */
			if (! strlen ( $usr_email )) {
				$this->_errors ['email_null'] = "Bạn vui lòng nhập địa chỉ email.";
			} else {
				if (! $this->_utilities->checkemail ( $usr_email )) {
					$this->_errors ['email_incorrect'] = "Địa chỉ email không hợp lệ.";
				}
			}
			
			/* Validate location */
			if ($usr_city == 0)
				$this->_errors ['city_null'] = "Bạn vui lòng chọn nơi bạn đang sống.";
				
			/* Validate captcha */
				if (!strlen($captcha)){
					$this->_errors ['captcha_null'] = "Bạn vui lòng nhập mã kiểm tra.";
				} else {
					$img = new Captcha_Securimage();
					$valid = $img->check($captcha);
					if (!$valid)
						$this->_errors ['captcha_incorrect'] = "Mã kiểm tra không đúng.";
				}
			
			
			/* if has error */
			if (count ( $this->_errors )) {
				$cities = $this->_utilities->getCity ();
				$account ['usr_birthday'] = $this->_utilities->formatdate ( $account ['usr_birthday'] );
				
				$this->view->assign ( "errors", $this->_errors );
				$this->view->assign ( "account", $account );
				$this->view->assign ( "cities", $cities );
				$this->view->assign ( "day", $day );
				$this->view->assign ( "month", $month );
				$this->view->assign ( "year", $year );
				
				$this->render ( "register-account" );
				return;
			}
			// End validate.
			

			$userObject->insertUser ( $account );
			
			$this->_redirect ( $this->_contextPath . "/success" );
			return;
		}
		
		// Hien thi man hinh create member.
		$cities = $this->_utilities->getCity ();
		$account = array ('usr_mobile' => '', 'usr_password' => '', 'usr_fullname' => '', 'usr_birthday' => '', 'usr_email' => '', 'usr_activated' => '', 'usr_activated_date' => '', 'usr_role' => '', 'usr_created_date' => '', 'usr_coin_amount' => '', 'usr_city' => '' );
		
		$this->view->assign ( "account", $account );
		$this->view->assign ( "cities", $cities );
		
		$this->render ( "register-account" );
	}
	
	public function successAction() {
	}

}//End class.